<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/8/21
 * Time: 上午8:04
 */

namespace app\api\module;

use app\common\model\Club;
use app\common\model\DeveloperException;
use app\common\model\User;
use think\Db;
use app\common\model\UserException;

class Bussiness extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        if ($interface==H_BUSSINESS_SHARE)
        {
            return false;
        }else{
            return true;
        }

    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return string
     */
    public function exec($interface)
    {
        switch ($interface)
        {
            /**
             * @api {post} / 【1001】我的商会
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_my_bussiness
             * @apiGroup 商会【Bussiness】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} bussiness_id 商会id
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Bussiness",
             *   "interface": "1001",
             *   "data": {
             *     "access_token": "asdfafafadfasdfasdfad",
             *     "bussiness_id":"12"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {String} logo 商会图标
             * @apiSuccess (result_data-回执报文信息) {String} name 商会名称
             * @apiSuccess (result_data-回执报文信息) {String} all_member 商会所有成员
             * @apiSuccess (result_data-回执报文信息) {String} new_member 新入会的成员
             * @apiSuccess (result_data-回执报文信息) {String} follow_member 被关注数量
             * @apiSuccess (result_data-回执报文信息) {String} introduce 商会简介
             * @apiSuccess (result_data-回执报文信息) {Array} lader_list 商会领导列表
             * @apiSuccess (lader_list-回执报文信息) {String} avatar 领导头像
             * @apiSuccess (lader_list-回执报文信息) {String} name 姓名
             * @apiSuccess (lader_list-回执报文信息) {String} position 领导商会职位
             * @apiSuccess (lader_list-回执报文信息) {String} extra 介绍
             * @apiSuccess (result_data-回执报文信息) {Array} member_list 商会会员列表
             * @apiSuccess (member_list-回执报文信息) {String} avatar 会员头像
             * @apiSuccess (member_list-回执报文信息) {String} name 姓名
             * @apiSuccess (member_list-回执报文信息) {String} position 会员部门
             * @apiSuccess (member_list-回执报文信息) {String} mobile 会员手机号
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *      "logo": "123456",
             *       "name": "123456",
             *       "all_member": "100",
             *       "new_member": "10",
             *       "follow_member": "1212",
             *        "introduce":"简介",
             *       "lader_list":[
             *            {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"会长",
             *                 "extra":"天元董事长",
             *             }
             *           ],
             *       "member_list":[
             *            {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"会长",
             *                 "mobile":"18854485466",
             *             }
             *           ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "我的商户首页！"
             * }
             *
             */
            case H_MY_BUSSINESS:
                return $this->my_bussiness();
            /**
             * @api {post} / 【1002】领导列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_lader_list
             * @apiGroup 商会【Bussiness】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} bussiness_id 商会id
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Bussiness",
             *   "interface": "1002",
             *   "data": {
             *     "access_token": "6a204bd89f3c8348afd5c77c717a097a",
             *     "bussiness_id": "1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} lader_list 领导列表
             * @apiSuccess (lader_list-回执报文信息) {String} avatar 领导头像
             * @apiSuccess (lader_list-回执报文信息) {String} name 姓名
             * @apiSuccess (lader_list-回执报文信息) {String} position 领导商会职位
             * @apiSuccess (lader_list-回执报文信息) {String} extra 介绍
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "lader_list":[
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"会长",
             *                 "extra":"天元董事长",
             *         },
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"会长",
             *                 "extra":"天元董事长",
             *         },
             *      ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "领导列表！"
             * }
             *
             */
            case H_BUSSINESS_LADER_LIST:
                return $this->leader_list();
            /**
             * @api {post} / 【1003】会员列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_member_list
             * @apiGroup 商会【Bussiness】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} bussiness_id 商会id
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Bussiness",
             *   "interface": "1003",
             *   "data": {
             *     "access_token": "6a204bd89f3c8348afd5c77c717a097a",
             *     "page": "1",
             *      "page_size": "3",
             *     "bussiness_id": "1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} member_list 会员列表
             * @apiSuccess (member_list-回执报文信息) {String} avatar 会员头像
             * @apiSuccess (member_list-回执报文信息) {String} name 姓名
             * @apiSuccess (member_list-回执报文信息) {String} position 哪个部门
             * @apiSuccess (member_list-回执报文信息) {String} mobile 手机号
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "member_list":[
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"技术部",
             *                 "mobile":"18853926512",
             *                 "is_follow":"1"
             *         },
             *         {
             *                 "avatar":"http://12121",
             *                 "name":"yuhan",
             *                 "position":"会长",
             *                 "mobile":"1888555451",
             *                 "is_follow":"1"
             *         },
             *      ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "会员列表！"
             * }
             *
             */
            case H_BUSSINESS_MEMBER_LIST:
                return $this->member_list();
            /**
             * @api {post} / 【1004】会员详情
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_member_info
             * @apiGroup 商会【Bussiness】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} uid 用户id
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Bussiness",
             *   "interface": "1004",
             *   "data": {
             *     "access_token": "13355064521",
             *     "uid": "123456"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data":
             *    {
             *         "avatar":"touxiang",
             *         "name":"姓名",
             *         "position":"商会职位",
             *         "extra":"自己公司职位",
             *         "mobile":"18265156536",
             *         "email":"邮箱",
             *         "branch":"部门",
             *         "service":"服务内容",
             *
             *    }
             *   "result_code": "0000",
             *   "result_info": "会员详情！"
             * }
             *
             */
            case H_BUSSINESS_MEMBER_INFO:
                return $this->member_info();
            /**
             * @api {post} / 【1005】商会列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_list
             * @apiGroup 商会【Bussiness】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             * @apiParam (dataGroup) {String} type 类别（1：轮播，2：全部）
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Bussiness",
             *   "interface": "1005",
             *   "data": {
             *     "access_token": "6a204bd89f3c8348afd5c77c717a097a",
             *     "type": "1",
             *     "page": "1",
             *      "page_size": "3"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} bussiness_list 商会列表
             * @apiSuccess (bussiness_list-回执报文信息) {String} logo 商会图标
             * @apiSuccess (bussiness_list-回执报文信息) {String} name 商会名称
             * @apiSuccess (bussiness_list-回执报文信息) {String} manager 会长
             * @apiSuccess (bussiness_list-回执报文信息) {String} mobile 手机号
             * @apiSuccess (bussiness_list-回执报文信息) {String} address 商会地址
             * @apiSuccess (bussiness_list-回执报文信息) {String} member_num 会员数量
             * @apiSuccess (bussiness_list-回执报文信息) {String} is_follow 是否关注
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "bussiness_list":[
             *         {
             *                 "logo":"http://12121",
             *                 "name":"yuhan",
             *                 "manager":"会长",
             *                 "mobile":"18853926512",
             *                 "address":"沂蒙路",
             *                 "member_num":"20",
             *                 "is_follow":"1"
             *         },
             *         {
             *                 "logo":"http://12121",
             *                 "name":"yuhan",
             *                 "manager":"会长",
             *                 "mobile":"18853926512",
             *                 "address":"沂蒙路",
             *                 "member_num":"20",
             *                 "is_follow":"1"
             *         }
             *      ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "商会列表！"
             * }
             *
             */
            case H_BUSSINESS_LIST:
                return $this->bussiness_list();
            /**
             * @api {post} / 【1006】查看手机号
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_show_mobile
             * @apiGroup 商会【Bussiness】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} user_id 要查询的会员id
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Bussiness",
             *   "interface": "1006",
             *   "data": {
             *     "access_token": "6a204bd89f3c8348afd5c77c717a097a",
             *     "user_id": "1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {string} mobile 会员手机号
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *       "mobile":"15469978235"
             *   },
             *   "result_code": "0000",
             *   "result_info": "会员手机号！"
             * }
             *
             */
            case H_BUSSINESS_SHOW_MOBILE:
                return $this->show_mobile();
            /**
             * @api {post} / 【1007】互通合约
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_agreement_list
             * @apiGroup 商会【Bussiness】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token token
             * @apiParam (dataGroup) {String} type 类型（1：我发出的，2：别人请求我的）
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Bussiness",
             *   "interface": "1007",
             *   "data": {
             *     "access_token": "6a204bd89f3c8348afd5c77c717a097a",
             *     "type":"1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {string} avatar 头像
             * @apiSuccess (result_data-回执报文信息) {string} tel 手机号
             * @apiSuccess (result_data-回执报文信息) {string} bussiness_id 商会id（请求或者被请求）
             * @apiSuccess (result_data-回执报文信息) {string} status 状态
             * @apiSuccess (result_data-回执报文信息) {string} create_time 请求时间
             *
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "agreement_list":[
             *         {
             *                 "avatar":"http://12121",
             *                 "tel":"18265156352",
             *                 "bussiness_id":"12",
             *                 "status":"0",
             *                 "create_time":"1"
             *         },
             *         {
             *                 "avatar":"http://12121",
             *                 "tel":"18265156352",
             *                 "bussiness_id":"12",
             *                 "status":"0",
             *                 "create_time":"1"
             *         }
             *      ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "合约记录！"
             * }
             *
             */
            case H_BUSSINESS_AGREEMENT_LIST:
                return $this->agreement_list();
            /**
             * @api {post} / 【1008】会长分享
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName bussiness_share
             * @apiGroup 商会【Bussiness】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} mobile 上级会员手机号
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Bussiness",
             *   "interface": "1008",
             *   "data": {
             *     "phone": "110"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {string} mobile 会员手机号
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *       "mobile":"15469978235",
             *       "url":"15469978235",
             *       "title":"15469978235",
             *       "avatar":"15469978235",
             *   },
             *   "result_code": "0000",
             *   "result_info": "分享成功！"
             * }
             *
             */
            case H_BUSSINESS_SHARE:
                return $this->share();
            default:
                send_developer_error("模块【商会】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    /**
     * 商会列表
     *
     */
    public function bussiness_list()
    {
        $access_token=$this->essentialParam("access_token");
        $page=$this->essentialParam("page");
        $page_size=$this->essentialParam("page_size");
        $type=$this->essentialParam("type");

        $user=User::userFromToken($access_token);
        $list=Club::bussinessList($type,$page,$page_size,$user);

        $this->insertResponse("list",$list);
        return $this->response('商会列表!');

    }

    /**
     * 商会信息
     *
     */
    public function my_bussiness()
    {
        $access_token=$this->essentialParam("access_token");
        //商会id
        $bussiness_id=$this->essentialParam("bussiness_id");

        /*用户信息*/
        $user=User::userFromToken($access_token);

        /*判断是否激活*/
        if($user['identity']==1&&$bussiness_id!=$user['bussiness_id'])
        {
            throw new DeveloperException('您还没有激活，不能查看其他商会信息！');
        }

        $info=Club::bussinessInfo($bussiness_id);
        $this->insertResponse('info',$info);
        return $this->response('商会信息');
    }

    /**
     * 领导列表
     *
     */
    public function leader_list()
    {
        $access_token=$this->essentialParam("access_token");
        $bussiness_id=$this->essentialParam("bussiness_id");

        $user=User::userFromToken($access_token);
        $list=Club::leaderList($bussiness_id);

        $this->insertResponse("list",$list);
        return $this->response('领导列表!');

    }

    /**
     * 会员列表
     *
     */
    public function member_list()
    {
        $access_token=$this->essentialParam("access_token");
        $bussiness_id=$this->essentialParam("bussiness_id");
        $page=$this->essentialParam("page");
        $page_size=$this->essentialParam("page_size");

        $user=User::userFromToken($access_token);
        $list=Club::bussinessMemberList(2,$page,$page_size,$bussiness_id);

        $this->insertResponse("list",$list);
        return $this->response('会员列表!');

    }

    /**
     * 会员信息
     *
     */
    public function member_info()
    {
        $access_token=$this->essentialParam("access_token");
        //要查询会员id
        $uid=$this->optionalParam("uid");
        /*登录的会员*/
        $user=User::userFromToken($access_token);
        if(empty($uid)){
            $uid = $user['id'];
        }
        $info=User::memberInfo($uid);
        $this->insertResponse('info',$info);
        return $this->response('会员详细信息');
    }

    /**
     * 会员手机号
     *
     */
    public function show_mobile()
    {
        $access_token=$this->essentialParam("access_token");
        //要查询会员id
        $user_id=$this->essentialParam("user_id");
        /*登录的会员*/
        $user=User::userFromToken($access_token);

        /*要查询的会员信息*/
        $member=User::exists($user_id);

        if($user['bussiness_id']==$member['bussiness_id'])
        {
            /*本商会会员*/
            $mobile=$member['mobile'];
        }else{
            /*商会信息*/
            $bussiness=Club::bussinessInfoById($member['bussiness_id']);
            if($user['duties_id']==1)
            {
                /*查看是否有申请*/
                if($user['bussiness_id']>$member['bussiness_id'])
                {
                    $tag=$member['bussiness_id'].",".$user['bussiness_id'];
                }else{
                    $tag=$user['bussiness_id'].",".$member['bussiness_id'];
                }

                $apply=Db::name('bussiness_agreement')
                    ->where('tag',$tag)
                    ->field('id,status')
                    ->find();

                if(empty($apply))
                {
                    /*未申请过*/
                    $mobile="";
                    $this->insertResponse('bussiness_id',$user['bussiness_id']);
                }elseif($apply['status']==1)
                {
                    /*已同意*/
                    $mobile=$member['mobile'];
                }else{
                    /*未同意*/
                    throw new DeveloperException('您已经申请过查看对方商会信息，等待对方同意！');
                }

            }else{
                throw new DeveloperException('您没有权利查看此号码！');
            }
        }

        $this->insertResponse('mobile',$mobile);
        return $this->response('手机号');
    }

    /**
     * 互通合约列表
     *
     */
    public function agreement_list()
    {
        $access_token=$this->essentialParam("access_token");

        $type=$this->essentialParam("type");

        $user=User::userFromToken($access_token);
        $list=Club::agreementList($type,$user);

        $this->insertResponse("list",$list);
        return $this->response('互通合约列表!');

    }
    /**
     * 会长分享
     *
     */
    public function share()
    {
        $mobile=$this->essentialParam("phone");
        $user=User::exists($mobile);
       $info = Db::name('user')
            ->alias('u')
            ->join("__BUSSINESS__ b","u.bussiness_id=b.id")
            ->where('u.mobile',$mobile)
            ->field("u.id as id,u.real_name as real_name,u.mobile as mobile,u.bussiness_id as bussiness_id,b.title as title,b.avatar as avatar")
            ->find();
       $info['avatar']="http://".$_SERVER['HTTP_HOST'].$info['avatar'];
        $url="http://hlapp.liumaikeji.com".url('/Login/register',array('phone'=>$mobile,'bussiness_id'=>$info['bussiness_id']));
        $content=$info['real_name']." 会长诚邀您加入".$info['title'];
        $this->insertResponse('title',$info['title']);
        $this->insertResponse('url',$url);
        $this->insertResponse('avatar',$info['avatar']);
        $this->insertResponse('content',$content);
        return $this->response('分享成功!');

    }
}